/**
 ******************************************************************************
 * @file      dddd.c
 * @brief     C Source file of dddd.c.
 * @details   This file including all API functions's implement of dddd.c.
 * @copyright  Copyright(C)
 ******************************************************************************
 */

/*------------------------------------------------------------------------------
 Section: Includes
 ------------------------------------------------------------------------------*/
#include <drivers/pin.h>
#include "led.h"


/*------------------------------------------------------------------------------
 Section: Macro Definitions
 ------------------------------------------------------------------------------*/
#define  HC574_PORT  *(uint32_t *)0x60001000

/*------------------------------------------------------------------------------
 Section: Type Definitions
 ------------------------------------------------------------------------------*/
/* None */

/*------------------------------------------------------------------------------
 Section: Global Function prototypes
 ------------------------------------------------------------------------------*/
/* None */

/*------------------------------------------------------------------------------
 Section: Local Function prototypes
 ------------------------------------------------------------------------------*/
void Led_Thread(void);

/*------------------------------------------------------------------------------
 Section: Global Variables
 ------------------------------------------------------------------------------*/
/* None */

/*------------------------------------------------------------------------------
 Section: Local Variables
 ------------------------------------------------------------------------------*/
volatile uint32_t g_HC574;   /* 保存74HC574端口状态 */

/*------------------------------------------------------------------------------
 Section: Function Definitions
 ------------------------------------------------------------------------------*/
/**
 ******************************************************************************
 * @brief       Led_Thread
 * @param
 * @return
 * @details
 ******************************************************************************
 */
void Led_Thread(void)
{
    Led_Init();

    while(1)
    {
        Led_On();
        rt_kprintf("READ_PIN: %d.", rt_pin_read(LED1) );
        rt_thread_mdelay(1000);
//        Led_Off();
        rt_kprintf("READ_PIN: %d.", rt_pin_read(LED1) );
        rt_thread_mdelay(1000);
    }
}

/**
 ******************************************************************************
 * @brief       Led_Init
 * @param
 * @return
 * @details
 ******************************************************************************
 */
void Led_Init(void)
{
    rt_pin_mode(LED1, PIN_MODE_OUTPUT);
    rt_pin_mode(LED2, PIN_MODE_OUTPUT);
    rt_pin_mode(LED3, PIN_MODE_OUTPUT);
    rt_pin_mode(LED4, PIN_MODE_OUTPUT);
}

/**
 ******************************************************************************
 * @brief       Led_On
 * @param
 * @return
 * @details
 ******************************************************************************
 */
void Led_On(void)
{
    HC574_SetPin(LED1, PIN_LOW);
    HC574_SetPin(LED2, PIN_LOW);
    HC574_SetPin(LED3, PIN_LOW);
    HC574_SetPin(LED4, PIN_LOW);
}

/**
 ******************************************************************************
 * @brief       Led_Off
 * @param
 * @return
 * @details
 ******************************************************************************
 */
void Led_Off(void)
{
    HC574_SetPin(LED1, PIN_HIGH);
    HC574_SetPin(LED2, PIN_HIGH);
    HC574_SetPin(LED3, PIN_HIGH);
    HC574_SetPin(LED4, PIN_HIGH);
}

/**
 ******************************************************************************
 * @brief       Led_Toggle
 * @param
 * @return
 * @details
 ******************************************************************************
 */
void Led_Toggle(uint32_t _pin)
{
    if (HC574_GetPin(_pin))
   {
       HC574_SetPin(_pin, 0);
   }
   else
   {
       HC574_SetPin(_pin, 1);
   }
}

/**
 ******************************************************************************
 * @brief       HC574_SetPin
 * @param
 * @return
 * @details
 ******************************************************************************
 */
void HC574_SetPin(uint32_t _pin, uint8_t _value)
{
    if (_value == 0)
    {
        g_HC574 &= (~_pin);
    }
    else
    {
        g_HC574 |= _pin;
    }
    HC574_PORT = g_HC574;
}

/*
*********************************************************************************************************
*   函 数 名: HC574_GetPin
*   功能说明: 判断指定的管脚输出是1还是0
*   形    参: _pin : 管脚号， 0-31; 只能选1个，不能多选
*   返 回 值: 0或1
*********************************************************************************************************
*/
uint8_t HC574_GetPin(uint32_t _pin)
{
    if (g_HC574 & _pin)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}



/*------------------------------End of dddd.c----------------------------*/
